package com.lwz.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lwz.eduservice.client.VodClient;
import com.lwz.eduservice.entity.EduCourse;
import com.lwz.eduservice.entity.EduVideo;
import com.lwz.eduservice.mapper.EduVideoMapper;
import com.lwz.eduservice.service.EduVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lwz.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author Lw中
 * @since 2020-11-11
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {

    /**注入删除多个视频的接口*/
    @Autowired
    VodClient vodClient;

    /**
     * LWZ TODO : 2020/11/14 删除小节的视频
     */
    @Override
    public void removeVideoByCourseId(String courseId) {
        // 根据课程id查询所有的视频id
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        // 查询course_id符合的视频id：video——source_id，再通过视频list集合删除所有的视频
        wrapper.eq("course_id", courseId);
        wrapper.select("video_source_id");
        List<EduVideo> eduVideoList = baseMapper.selectList(wrapper);
        // 将eduVideoList中的视频id读取出来放到另一个list中
        List<String> videoIds = new ArrayList<>();
        for (int i = 0; i < eduVideoList.size(); i++) {
            EduVideo eduVideo = eduVideoList.get(i);
            String id = eduVideo.getVideoSourceId();
            // 如果id值不为空将videoid添加至videoIds中
            if (!StringUtils.isEmpty(id)) {
                videoIds.add(id);
            }
        }
        // 删除多个视频
        if (videoIds.size() > 0) {
            vodClient.deleteBatch(videoIds);
        }
        QueryWrapper<EduVideo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id", courseId);
        baseMapper.delete(queryWrapper);
    }
}
